/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.swing.table;

import java.util.*;
import java.sql.*;

import jdbcmanager.app.*;

/**
 * This table copies all information from a ResultSet into an internal Vector 
 * (this is what is shown).
 *  
 * @author Francisco Morero Peyrona
 */
public class JxTableRamRowSet extends JxTableVector
{
   public JxTableRamRowSet() throws SQLException
   {
      this( null );
   }
   
   public JxTableRamRowSet( ResultSet rs ) throws SQLException
   {
      super();
      
      if( rs != null )
         setData( rs );
   }
   
   public void setData( ResultSet rs ) throws SQLException
   {
      ResultSetMetaData rsmd  = rs.getMetaData();
      int               nCols = rsmd.getColumnCount();
      Vector            vData = new Vector();
      Vector            vHead = new Vector();
    
      // Add columns titles
      for( int n = 1; n <= nCols; n++ )
         vHead.add( StrWise.capitalize( rsmd.getColumnLabel( n ), true ) );
               
      // Add data rows
      while( rs.next() )
      {
         Vector vRow = new Vector( nCols ); 
         
         for( int n = 1; n <= nCols; n++ )
         {
            Object obj = rs.getObject( n );
            vRow.add( (obj == null) ? "<null>" : obj );
         }
         
         vData.add( vRow );
      }
    
      // Call super
      setData( vData );
      setHead( vHead );
      
      adjustColumnsWidth();
      
      setEditable( false );     
   }
}